package com.demo.datastructure.array;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @description: letcode 15 三数之和
 * @author: ljr
 * @time: 2021/5/2 17:53
 * @since 1.2
 */
public class ThreeSumArray3 {

    public List<List<Integer>> threeSum(int[] nums) {
        int length = nums.length;
        if(length < 3){
             return new ArrayList();
        }
        List<List<Integer>> list = new ArrayList<>();
        if(length == 3){
            if(nums[0]+nums[1]+nums[2] == 0){
                List<Integer> sonList = new ArrayList<>();
                sonList.add(nums[0]);
                sonList.add(nums[1]);
                sonList.add(nums[2]);
                list.add(sonList);
                return list;
            }
            return new ArrayList();
        }

        Arrays.sort(nums);

        int newLength = length-2;
        for(int c = 0; c < newLength; c++) {
            int num1 = nums[c];
            if(num1 > 0){
                break;
            }
            if(c != 0 && num1 == nums[c-1]){
                continue;
            }
            int L = c+1, R = nums.length-1;
            while(L < R){
                int tmp = num1 + nums[L] + nums[R];
                if(tmp == 0) {
                    List<Integer> sonList = new ArrayList<>();
                    sonList.add(num1);
                    sonList.add(nums[L]);
                    sonList.add(nums[R]);
                    list.add(sonList);
                    while(L < R && nums[L+1] == nums[L]) ++L;
                    while (L < R && nums[R-1] == nums[R]) --R;
                    ++L;
                    --R;
                } else if(tmp < 0) {
                    ++L;
                } else {
                    --R;
                }
            }

        }
        return list;
    }


    public static void main(String[] args) {
        ThreeSumArray3 threeSumArray = new ThreeSumArray3();
        int[] nums = new int[]{-1,0,1,2,-1,-4,-2,-3,3,0,4};
        List<List<Integer>> list= threeSumArray.threeSum(nums);
        System.out.println("1");
    }
}
